package producer

import (
	"encoding/json"

	"gitee.com/shiwjlinux/shoplink/config"
	"gitee.com/shiwjlinux/shoplink/models"
	"gitee.com/shiwjlinux/shoplink/utils"
)

func ProduceOrder(order models.Order) error {
	conn, ch, err := utils.ConnectToRabbitMQ()
	if err != nil {
		return err
	}

	defer conn.Close()
	defer ch.Close()

	_, err = utils.DeclareQueue(ch, config.OrderQueue)
	if err != nil {
		return err
	}

	orderBytes, _ := json.Marshal(order)

	err = utils.PublishMessage(ch, config.OrderQueue, orderBytes)
	if err != nil {
		return err
	}
	return nil
}
